package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.PlayBackBean;
import com.shqcjd.sinopecdemomobile.ui.adapter.PlaybackAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.WrapContentLinearLayoutManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class PlaybackActivity extends BaseActivity {
    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.rv_playback)
    RecyclerView rvPlayback;
    @BindView(R.id.sr_playback)
    SwipeRefreshLayout srPlayback;
    @BindView(R.id.rl_isnull)
    RelativeLayout rlIsnull;

    private Context context;
    private PlaybackAdapter playbackAdapter;
    private List<PlayBackBean.ItemsBean> mList = new ArrayList<>();

    private int tId, inspectId;
    private int page = 1;
    private int isRefreshOrLoadMore = 0;//0代表刷新  1代表加载更多

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_playback;
    }

    @Override
    public void init() {
        super.init();
        tId = getIntent().getIntExtra("tId", 1);
        inspectId = getIntent().getIntExtra("inspectId", -1);//如果这里有值  说明是地图页面的巡检点跳过来的
    }

    @Override
    public void initView() {
        super.initView();
        context = this;
        tvIncludeTitle.setText("录像回放");

        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        srPlayback.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        refreshData();
    }

    private void refreshData() {
        refreshOrLoadMore();
        srPlayback.setRefreshing(true);
        isRefreshOrLoadMore = 0;
        page = 1;
        mList.clear();
        getData(page);
    }

    private void loadMoreData() {
        isRefreshOrLoadMore = 1;
        getData(page + 1);
    }

    private void refreshOrLoadMore(){
        if(playbackAdapter!=null){
            playbackAdapter.notifyLoadMoreToLoading();
        }
    }


    private void getData(int p) {
        Map<String, String> map = new HashMap<>();
        map.put("pageIndex", p+"");
        map.put("pageSize", "4");
        map.put("id", tId+"");
        if(inspectId!=-1){
            map.put("pointId", inspectId+"");
            ApiService.getPointInspectInfo(new JSONObject(map))
                    .compose(RxSchedulersHelper.io_main())
                    .subscribe(new RxObserver<ResponseData<PlayBackBean>>() {
                        @Override
                        public void _onSubscribe(Disposable d) {
                            super._onSubscribe(d);
                        }
                        @Override
                        public void _onNext(ResponseData<PlayBackBean> responseData) {
                            if(responseData.retCode!=10000){
                                return;
                            }
                            mList.addAll(responseData.data.items);
                            if (playbackAdapter == null) {
                                rvPlayback.setLayoutManager(new WrapContentLinearLayoutManager(context));
                                playbackAdapter = new PlaybackAdapter(mList,context, tId);
                                rvPlayback.setAdapter(playbackAdapter);

                                playbackAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                    @Override
                                    public void onLoadMoreRequested() {
                                        rvPlayback.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                int pageSize = responseData.data.total%4==0?responseData.data.total/4:responseData.data.total/4+1;
                                                if (page >= pageSize) {
                                                    playbackAdapter.loadMoreEnd();
                                                } else {
                                                    loadMoreData();
                                                }
                                            }
                                        });
                                    }
                                });
                            }
                            if (isRefreshOrLoadMore == 1) {
                                page = page + 1;
                                playbackAdapter.loadMoreComplete();
                            }

                            if(mList.size()==0){
                                rlIsnull.setVisibility(View.VISIBLE);
                            }else{
                                rlIsnull.setVisibility(View.INVISIBLE);
                            }
                            playbackAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void _onError(String errorMessage) {
                            Log.e("tou", errorMessage);
                            hideWaitingDialog();
                        }

                        @Override
                        public void onComplete() {
                            super.onComplete();
                            srPlayback.setRefreshing(false);
                        }
                    });
        }else{
            ApiService.playBackHttp(new JSONObject(map))
                    .compose(RxSchedulersHelper.io_main())
                    .subscribe(new RxObserver<ResponseData<PlayBackBean>>() {
                        @Override
                        public void _onSubscribe(Disposable d) {
                            super._onSubscribe(d);
                        }
                        @Override
                        public void _onNext(ResponseData<PlayBackBean> responseData) {
                            if(responseData.retCode!=10000){
                                return;
                            }
                            mList.addAll(responseData.data.items);
                            if (playbackAdapter == null) {
                                rvPlayback.setLayoutManager(new WrapContentLinearLayoutManager(context));
                                playbackAdapter = new PlaybackAdapter(mList,context, tId);
                                rvPlayback.setAdapter(playbackAdapter);

                                playbackAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                    @Override
                                    public void onLoadMoreRequested() {
                                        rvPlayback.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                int pageSize = responseData.data.total%4==0?responseData.data.total/4:responseData.data.total/4+1;
                                                if (page >= pageSize) {
                                                    playbackAdapter.loadMoreEnd();
                                                } else {
                                                    loadMoreData();
                                                }
                                            }
                                        });
                                    }
                                });
                            }
                            if (isRefreshOrLoadMore == 1) {
                                page = page + 1;
                                playbackAdapter.loadMoreComplete();
                            }

                            if(mList.size()==0){
                                rlIsnull.setVisibility(View.VISIBLE);
                            }else{
                                rlIsnull.setVisibility(View.INVISIBLE);
                            }
                            playbackAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void _onError(String errorMessage) {
                            Log.e("tou", errorMessage);
                            hideWaitingDialog();
                        }

                        @Override
                        public void onComplete() {
                            super.onComplete();
                            srPlayback.setRefreshing(false);
                        }
                    });
        }
    }

}
